package com.kingtroldata.gyjk_demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by ljb on 2017/2/16.
 */

public class MonitorView1 extends FrameLayout {
    private static final String TAG = "MonitorView";
    private float design_width;
    private float design_height;
    private int bg_width;
    private int bg_height;
    private float scale_w;
    private float scale_h;

    public MonitorView1(Context context) {
        this(context, null);
    }

    public MonitorView1(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MonitorView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
    }

    /**
     * 设置模板尺寸
     *
     * @param design_width  设计的宽
     * @param design_height 设计的高
     */
    public void setDesign(float design_width, float design_height) {
        this.design_width = design_width;
        this.design_height = design_height;
        Log.i(TAG, "setDesign: 设置模板宽:" + design_width + ";高:" + design_height);
    }

    /**
     * 设置背景
     *
     * @param b Bitmap对象
     */
    public void addBackground(Bitmap b) {
        setBackground(new BitmapDrawable(b));
    }

    /**
     * 添加ImageView
     *
     * @param b Bitmap对象
     * @param w 宽
     * @param h 高
     * @param x x坐标
     * @param y y坐标
     */
    public void addImageView(Bitmap b, int w, int h, int x, int y) {
        models.add(new ViewModel(b, w, h, x, y));
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w > h) {
            //获取控件的大小
            bg_width = w;
            bg_height = h;
            Log.i(TAG, "onSizeChanged: MonitorView控件的宽:" + w + ";高:" + h);
            if (design_width == 0 || design_height == 0) {
                throw new IllegalStateException("请先设置模板尺寸!");
            }
            getScale();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw: onDraw方法调用");
        drawImage(canvas);
    }

    //画图片
    private void drawImage(Canvas canvas) {
        if (!models.isEmpty()) {
            for (ViewModel model : models) {
                Bitmap b = model.b;
                int w = model.w;
                int h = model.h;
                int x = model.x;
                int y = model.y;
                int w_t = (int) (w * scale_w);
                int h_t = (int) (h * scale_h);
                int x_t = (int) (x * scale_w);
                int y_t = (int) (y * scale_h);
                Rect srcRect = new Rect(0, 0, b.getWidth(), b.getHeight());
                Rect dstRect = new Rect(x_t, y_t, x_t + w_t, y_t + h_t);
                canvas.drawBitmap(b, srcRect, dstRect, null);
                Log.i(TAG, "drawImage: 添加了一张图片");
            }
            Log.i(TAG, "drawImage: 刷新完毕");
        }
    }

    /**
     * 获取x轴,y轴的缩放比例
     */
    private void getScale() {
        scale_w = bg_width / design_width;
        scale_h = bg_height / design_height;
        Log.i(TAG, "getScale: x轴缩放比:" + scale_w + ";y轴缩放比:" + scale_h);
    }

    private CopyOnWriteArrayList<ViewModel> models = new CopyOnWriteArrayList<>();

    static class ViewModel {
        Bitmap b;
        int w;
        int h;
        int x;
        int y;

        ViewModel(Bitmap b, int w, int h, int x, int y) {
            this.b = b;
            this.w = w;
            this.h = h;
            this.x = x;
            this.y = y;
        }
    }
}
